package com.labyrinth.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;

public class Brick extends GameObject
{
	private int direction; // Direction of the brick
	private SpriteObject arrow;

	Brick()
	{
	}

	Brick(float x, float y, float height, float width, int inDirection)
	{
		super(x, y);
		this.setScale(width, height);
		if (inDirection == -1)
		{
			setTexture("data/Floor.jpg");
		}
		else
		{
			setTexture("data/brick.jpg");
		}
		setDrawType(GL10.GL_TRIANGLE_STRIP);
		setMesh();
		arrow = new SpriteObject("data/arrow.png", position.getX(), position.getY());
		direction = inDirection;

		if (direction != -1 && direction != 0)
		{
			arrow.rotate(direction - 1);
		}
	}

	public void setMesh()
	{
		mesh = new Mesh(true, 4, 4, new VertexAttribute(Usage.Position, 3, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord"));
		mesh.setVertices(new float[] { -0.5f, -0.5f, 0, 0, 1, 0.5f, -0.5f, 0, 1, 1, -0.5f, 0.5f, 0, 0, 0, 0.5f, 0.5f, 0, 1, 0 });
		mesh.setIndices(new short[] { 0, 1, 2, 3 });
		vertexCount = 4;

	}

	public void setPosition(float x, float y)
	{
		this.position.setX(x);
		this.position.setY(y);

		arrow.setPosition(x, y);
	}

	@Override
	public void draw()
	{
		Gdx.gl10.glPushMatrix();
		Gdx.gl10.glTranslatef(position.getX(), position.getY(), 0);
		Gdx.gl10.glScalef(scaleX, scaleY, 0);
		Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D);
		texture.bind();
		mesh.render(drawType, 0, vertexCount);
		
		if (direction != -1 && arrow != null)
		{
			arrow.draw();
		}
		Gdx.gl10.glPopMatrix();
	}

	public int getDirection()
	{
		return this.direction;
	}

	void setDirection(int direction)
	{
		setTexture("data/brick.jpg");
		this.direction = direction;

		if (direction != -1 && direction != 0)
		{
			arrow.rotate(direction - 1);
		}
	}

}
